package mianshi;



/**
 * Created by JiaJia on 2019/8/13 0013.
 */
public class ABCPrint implements Runnable {
    private String name;
    private Object prev;
    private Object self;

    private ABCPrint(String name,Object prev,Object self){
        this.name=name;
        this.prev=prev;
        this.self=self;
    }

    @Override
    public void run() {
        int count=10;
        while (count>0){
            synchronized (prev){
                synchronized (self){
                    System.out.print(name);
                    count--;

                    self.notify();
                }
                try {
                    prev.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Object a=new Object();
        Object b=new Object();
        Object c=new Object();
        ABCPrint pa=new ABCPrint("A",c,a);
        ABCPrint pb=new ABCPrint("B",a,b);
        ABCPrint pc=new ABCPrint("C",b,c);

        new Thread(pa).start();
        Thread.sleep(100);
        new Thread(pb).start();
        Thread.sleep(100);
        new Thread(pc).start();
        Thread.sleep(100);

    }


}